from sqlalchemy import Boolean, Column, ForeignKey, Integer, String,DateTime,Float,Date
import datetime
from sqlalchemy.orm import relationship
from geoalchemy2 import Geometry
from .database import Base, SessionLocal # 已声明的数据库
from geoalchemy2.shape import to_shape
from geojson import Feature

"""row：行    column：列"""
from lisdemo.mygeotools.geodaisy import converters


class User(Base): 
    __tablename__ = "user"

    id = Column(Integer, unique=True,primary_key=True, index=True)
    hashed_password = Column(String)
    email = Column(String, unique=True, index=True)
    phone = Column(String, unique=True, index=True)
    borth=Column(Date,default=datetime.date.today())
    sex=Column(String)
    is_Infected=Column(Boolean,default=False)
    addressgeom = Column(Geometry('POINT'))
    workgeom=Column(Geometry('POINT'))
    temperature = Column(Float, nullable=False, default=37)
    issmoke=Column(Boolean,default=False)
    height=Column(Integer)
    weight=Column(Float)
    riskrank=Column(Float,default=0)
    items = relationship("Lineitem", backref="owner")
          # 关系
    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class Lineitem(Base):
    __tablename__ = "lineitem"
    item_id = Column(Integer, unique=True,primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("user.id"),primary_key=True)
    starttime=Column(DateTime, default=datetime.datetime.utcnow)
    endtime=Column(DateTime, default=datetime.datetime.utcnow)
    starttemperature=Column(Float,nullable=False,default=37)
    endtemperature = Column(Float, nullable=False, default=37)
    line=Column(Geometry('LINESTRING'))

    user = relationship("User", backref="lineitem")
    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
    def to_geojson(self):
        self_dict=self.to_dict()
        self_dict.pop("line")
        props=self_dict
        for item in props.items():
            if type(item[1])==datetime.datetime:
                props[item[0]]=item[1].isoformat()
        feature=Feature(geometry=to_shape(self.line), properties=props)
        return feature

class Pointitem(Base):
    __tablename__ = "pointitem"
    user_id = Column(Integer, ForeignKey("user.id"),primary_key=True)
    lineitem_id = Column(Integer, ForeignKey("lineitem.item_id"),primary_key=True)
    pointtime=Column(DateTime, default=datetime.datetime.utcnow,primary_key=True)
    point=Column(Geometry('Point'))

    user = relationship("User", backref="pointitem")
    lineitem = relationship("Lineitem", backref="pointitem")

    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
    def to_geojson(self):
        self_dict=self.to_dict()
        # print(to_shape(self.point).__geo_interface__)
        self_dict.pop("point")
        props=self_dict
        for item in props.items():
            if type(item[1])==datetime.datetime:
                props[item[0]]=item[1].isoformat()
        feature=Feature(geometry=converters.wkt_to_geo_interface(to_shape(self.point).wkt, 14), properties=props)
        # print(feature)
        return feature

class Patient(Base):
    __tablename__ = "patient"
    user_id = Column(Integer, ForeignKey("user.id"),primary_key=True)
    date_onset_symptoms=Column(DateTime, default=datetime.datetime.utcnow,primary_key=True)
    date_admission_hospital=Column(DateTime, default=datetime.datetime.utcnow,primary_key=True)
    date_confirmation=Column(DateTime, default=datetime.datetime.utcnow,primary_key=True)

    user = relationship("User", backref="patient")

    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

# Patient.__table__.drop(engine)
# Pointitem.__table__.drop(engine)
# Lineitem.__table__.drop(engine)
# User.__table__.drop(engine)
# User.__table__.create(engine)
# Lineitem.__table__.create(engine)
# Pointitem.__table__.create(engine)
# Patient.__table__.create(engine)


session=SessionLocal()
# user=User(id=0,hashed_password='2000020512lwt',email='3105833156@qq.com',phone='17396157105',addressgeom='POINT(6 '
#                                                                                                          '10)',
#           workgeom='POINT(6 10)')
# item=Lineitem(itemid=0,line='LINESTRING(120.19382669582967 30.258134,120.20382669582967 30.258134,120.19382669582967 '
#                             '30.348134)',user_id=0)
# pointitem=Pointitem(user_id=0,lineitem_id=0,point='POINT(6 10)')
# session.add_all([user,item,pointitem])
# session.commit()